﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlackBird.DataAccess;
using BlackBird.Models;
using System.Web.Mvc;

namespace BlackBird.ControllerHelpers
{
    public class BiJiaDanHelper
    {
        /// <summary>
        /// Get the product name and unit information list
        /// </summary>
        /// <returns></returns>
        public static ICollection<string> AllProductInformationList()
        {
            List<string> nameList = new List<string>();
            ProductDataAccess.ProductList.ForEach(p => nameList.Add(p.Name));

            return nameList;
        }


        public static string ProductUnit(string name)
        {
            var product = ProductDataAccess.ProductList.FirstOrDefault(p => p.Name.Equals(name));
            return product.Unit;
        }

        /// <summary>
        /// Get all company name and contact information
        /// </summary>
        /// <returns></returns>
        public static ICollection<string> AllCompanyInformationList()
        {
            List<string> nameList = new List<string>();
            ProductDataAccess.ProductList.ForEach(p => nameList.Add(p.Name));

            return nameList;
        }

        public static SupplierDM FindSupplier(string supplierName)
        {
            return SupplierDataAccess.SupplierList.First(p => p.Name.Equals(supplierName));
        }

        public static string GetProjectName(BiJiaDanDM bijiadan)
        {
            //有时候从view获得的dm数据不完整(比如缺少ParentId)，所以从runtime中获取完整的bijiadan
            var fullBijiadan = GetFullBijiadan(bijiadan.Id);

            ProjectDM project = ProjectDataAccess.GetProject(fullBijiadan.ParentId);
            if (project == null) throw new Exception("Failed to get Project Data Model");

            return project.BasicInfo.Name;
        }

        public static BiJiaDanDM GetFullBijiadan(string bijiadanId)
        {
            return BiJiaDanDataAccess.GetBiJiaDan(bijiadanId);
        }

		/// <summary>
		/// 生成比价单编号
		/// </summary>
		/// <param name="ProjectId"></param>
		/// <returns></returns>
		public static String GetBiJiaDanBianHao(string ProjectId)
		{
			var project = ProjectDataAccess.GetProject(ProjectId);
			string projectSuoXie = project.BasicInfo.SuoXie;
			int CurrentCount = BiJiaDanDataAccess.GetAllBijiadansCount(int.Parse(ProjectId)) + 1;
			int TodayCount = BiJiaDanDataAccess.GetTodayBijiadansCount() + 1;
			string CountToString = String.Format("{0:0000}", CurrentCount);
			string TodayToString = DateTime.Now.ToString("yyyyMMdd");
			string TodayCountToString = String.Format("{0:00}",TodayCount);

			return "C" + CountToString + "." + projectSuoXie + TodayToString + TodayCountToString;
		}

        public static string GetShenHeResultDisplayName(ShenHeResult result)
        {
            switch (result)
            {
                case ShenHeResult.Pass:
                    return Resources.BBStrings.ShenHeResult_Pass;
                case ShenHeResult.Fail:
                    return Resources.BBStrings.ShenHeResult_Fail;
                case ShenHeResult.ZuoFei:
                    return Resources.BBStrings.ShenHeResult_ZuoFei;
                case ShenHeResult.Undetermined:
                    return Resources.BBStrings.ShenHeResult_Undetermined;
            }

            return "";
        }
    }
}